extends RigidBody3D

@export var slot_data: SlotData
@export var player: CharacterBody3D

var model_node

func _ready():
	var item_data = slot_data.item_data
	model_node = item_data.model_scene.instantiate()
	add_child(model_node)

func player_interact():
	if player.inventory.pick_up_slot_data(slot_data):
		queue_free()

func save_slot():
	var dict: Dictionary = {
		"slot" : {
			"quantity": slot_data.quantity,
			"item_id": slot_data.item_data.id
		},
		"pos" : {
			"x" : position.x,
			"y" : position.y,
			"z" : position.z
		}
	}
	return dict

func load_slot(dict: Dictionary, items: Array[ItemData], new_player):
	var items_dict = {}
	for item in items:
		items_dict[item.id] = item
	var slot = SlotData.new()
	if items.has(dict['slot']['item_id']):
		slot.item_data = items_dict[dict['slot']['item_id']]
	else:
		slot.item_data = items_dict["corrupt"]
	slot.quantity = dict["slot"]['quantity']
	slot_data = slot
	position.x = dict["pos"]["x"]
	position.y = dict["pos"]["y"]
	position.z = dict["pos"]["z"]
	player = new_player
